@echo off
REM PPH系统健康监控脚本
REM 用于检查服务状态并在需要时重启

echo ========================================
echo PPH系统健康监控
echo ========================================
echo 检查时间: %date% %time%
echo.

REM 检查PM2服务状态
echo [1/4] 检查PM2服务状态...
pm2 list | findstr "online" >nul
if %errorlevel% neq 0 (
    echo [WARNING] PM2服务异常，尝试重启...
    cd /d "%~dp0.."
    pm2 restart all
    timeout /t 10 /nobreak >nul
)

REM 检查端口3011
echo [2/4] 检查后端端口3011...
netstat -an | findstr ":3011" | findstr "LISTENING" >nul
if %errorlevel% neq 0 (
    echo [ERROR] 端口3011未监听
    exit /b 1
) else (
    echo [OK] 端口3011正常监听
)

REM 检查端口3010
echo [3/4] 检查前端端口3010...
netstat -an | findstr ":3010" | findstr "LISTENING" >nul
if %errorlevel% neq 0 (
    echo [ERROR] 端口3010未监听
    exit /b 1
) else (
    echo [OK] 端口3010正常监听
)

REM 检查API健康状态
echo [4/4] 检查API健康状态...
curl -s "http://localhost:3011/api/health" --max-time 5 >nul
if %errorlevel% neq 0 (
    echo [ERROR] API健康检查失败
    exit /b 1
) else (
    echo [OK] API健康检查通过
)

echo.
echo ========================================
echo 所有检查通过，系统运行正常
echo ========================================
